package com.cloudsigns.factorymanager2.engineering.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.cloudsigns.factorymanager2.R;
import com.cloudsigns.factorymanager2.cusenum.CommonEnum;
import com.cloudsigns.factorymanager2.engineering.IView.ITeamReceiveOrBackEngineeringInstallOrderView;
import com.cloudsigns.factorymanager2.engineering.activity.TeamEngineeringInstallWorkorderInfoSurveyActivity;
import com.cloudsigns.factorymanager2.engineering.adapter.GridAdapter;
import com.cloudsigns.factorymanager2.engineering.bean.EngineeringInstallationOrderBean;
import com.cloudsigns.factorymanager2.engineering.presenter.TeamReceiveOrBackEngineeringInstallOrderPresenter;
import com.cloudsigns.factorymanager2.event.EngineeringOrderDetailForTeamEvent;
import com.cloudsigns.factorymanager2.project.adapter.ProductInformationAdapter;
import com.cloudsigns.factorymanager2.project.bean.ProductBean;
import com.cloudsigns.factorymanager2.project.utils.DateUtil;

import java.util.ArrayList;
import java.util.List;

import de.greenrobot.event.EventBus;

/**
 * 工单信息
 * Created by tony on 2018/4/6.
 */

public class WorkOrderInfomationForEngineeringInstallFragment extends Fragment implements ITeamReceiveOrBackEngineeringInstallOrderView, View.OnClickListener {
    private final String TAG = "WorkOrderInfomationForEngineeringInstallFragment";
    private TextView tv_project_name, tv_engineering_name;
    private TextView edt_engineering_address, edt_install_cycle, edt_receive_people_colon, edt_reception_time_colon; /*edt_dispath_order_person_colon, edt_dispath_order_time_colon, */
    ;
    //    private TextView edt_dispath_order_company_colon, edtcompletion_date,edt_order_statuse_colon,edt_tv_dispatch_status_colon, edt_wether_pause_colon, edt_start_date;
    private View ll_receive_back_btn;
    private View rl_project;//rl_wether_pause_colon;
    private GridView gridView;
    private TextView tv_survey_content;
    private GridAdapter gridAdapter;
    private List<String> picUrlList = new ArrayList<>();
    private TextView btn_receive_order, btn_back_order;
    private boolean isClick = true;
    private CommonEnum.STATUS status;
    private ListView lv;
    private LinearLayout ll_engineering_info_content;
    private ImageView img_project_select;
    private ProductInformationAdapter adapter;
    private List<ProductBean> productBeanList = new ArrayList<>();
    EngineeringInstallationOrderBean engineeringInstallationOrderBean;
    TeamReceiveOrBackEngineeringInstallOrderPresenter teamReceiveOrBackEngineeringInstallOrderPresenter;


    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        teamReceiveOrBackEngineeringInstallOrderPresenter = new TeamReceiveOrBackEngineeringInstallOrderPresenter(this);
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_engineeringinstall_workorderinfo_for_super, null);
        tv_project_name = view.findViewById(R.id.tv_project_name);
        tv_engineering_name = view.findViewById(R.id.tv_engineering_name);
        edt_engineering_address = view.findViewById(R.id.edt_engineering_address);
        edt_install_cycle = view.findViewById(R.id.edt_install_cycle);
//        edt_dispath_order_company_colon = view.findViewById(R.id.edt_dispath_order_company_colon);
//        edt_dispath_order_person_colon = view.findViewById(R.id.edt_dispath_order_person_colon);
//        edt_dispath_order_time_colon = view.findViewById(R.id.edt_dispath_order_time_colon);
//        edtcompletion_date = view.findViewById(R.id.edtcompletion_date);
//        edt_order_statuse_colon = view.findViewById(R.id.edt_order_statuse_colon);
        lv = view.findViewById(R.id.lv);
        ll_engineering_info_content = view.findViewById(R.id.ll_engineering_info_content);
        img_project_select = view.findViewById(R.id.img_project_select);
        edt_receive_people_colon = view.findViewById(R.id.edt_receive_people_colon);
        edt_reception_time_colon = view.findViewById(R.id.edt_reception_time_colon);
        rl_project = view.findViewById(R.id.rl_project);
        ll_receive_back_btn = view.findViewById(R.id.ll_receive_back_btn);
        tv_survey_content = view.findViewById(R.id.tv_survey_content);
//        edt_tv_dispatch_status_colon = view.findViewById(R.id.edt_tv_dispatch_status_colon);
//        rl_wether_pause_colon = view.findViewById(R.id.rl_wether_pause_colon);
//        edt_wether_pause_colon = view.findViewById(R.id.edt_wether_pause_colon);
//        edt_start_date = view.findViewById(R.id.edt_start_date);
        gridView = view.findViewById(R.id.gridView);
        gridView.setNumColumns(3);
        gridAdapter = new GridAdapter(getActivity());
        gridAdapter.setData(picUrlList);
        gridView.setAdapter(gridAdapter);
        btn_receive_order = view.findViewById(R.id.btn_receive_order);
        btn_back_order = view.findViewById(R.id.btn_back_order);


        img_project_select.setBackgroundResource(R.mipmap.arrow_down_icon);
        img_project_select.setOnClickListener(this);
        btn_receive_order.setOnClickListener(this);
        btn_back_order.setOnClickListener(this);
        tv_survey_content.setOnClickListener(this);

        adapter = new ProductInformationAdapter(getActivity(), TAG);
        adapter.setData(productBeanList);
        lv.setAdapter(adapter);

        return view;
    }

    public void initData(EngineeringInstallationOrderBean engineeringInstallationOrderBean) {
        if (null != engineeringInstallationOrderBean) {
            this.engineeringInstallationOrderBean = engineeringInstallationOrderBean;

            tv_project_name.setText(engineeringInstallationOrderBean.getProjectName());
            tv_engineering_name.setText("/" + engineeringInstallationOrderBean.getEngineeringName());
            edt_engineering_address.setText(engineeringInstallationOrderBean.getEngineeringAddress());
            edt_install_cycle.setText(DateUtil.getYyyy_Mm_Dd(engineeringInstallationOrderBean.getBeginDate()) + "/" + DateUtil.getYyyy_Mm_Dd(engineeringInstallationOrderBean.getFinishDate()));
//            edt_dispath_order_company_colon.setText(engineeringInstallationOrderBean.getCompanyName() + ":" + engineeringInstallationOrderBean.getCompanyId());
//            edt_dispath_order_person_colon.setText(engineeringInstallationOrderBean.getCreateOrderAccountName() + ":" + engineeringInstallationOrderBean.getCreateOrderAccountId());
//            edt_dispath_order_time_colon.setText(DateUtil.getYyyy_Mm_Dd(engineeringInstallationOrderBean.getSendOrderTime()));
//            edt_start_date.setText(DateUtil.getYyyy_Mm_Dd(engineeringInstallationOrderBean.getBeginDate()));
//            edtcompletion_date.setText(DateUtil.getYyyy_Mm_Dd(engineeringInstallationOrderBean.getCompleteDate()));
            edt_receive_people_colon.setText(engineeringInstallationOrderBean.getReceiveAccountName() + ":" + engineeringInstallationOrderBean.getReceiveAccountId());
            edt_reception_time_colon.setText(DateUtil.getYyyy_Mm_Dd(engineeringInstallationOrderBean.getReceiveOrderTime()));
            List<String> urlLIst = engineeringInstallationOrderBean.getPicUrl();
            if (null != urlLIst) {
                picUrlList.clear();
                picUrlList.addAll(urlLIst);
                gridAdapter.setData(picUrlList);
                gridAdapter.notifyDataSetChanged();
            }
            CommonEnum.EngineeringOrderStatus engineeringOrderStatus = engineeringInstallationOrderBean.getStatus();
            if (null != engineeringOrderStatus) {
                if (engineeringOrderStatus == CommonEnum.EngineeringOrderStatus.CREATED) {
//                    edt_order_statuse_colon.setText("[创建/未编辑]");
                    isClick = true;
                } else if (engineeringOrderStatus == CommonEnum.EngineeringOrderStatus.EDITED) {
//                    edt_order_statuse_colon.setText("[已编辑/未派发]");
                    isClick = true;
                } else if (engineeringOrderStatus == CommonEnum.EngineeringOrderStatus.SEND) {
//                    edt_order_statuse_colon.setText("[派发中]");
                    isClick = false;
                } else if (engineeringOrderStatus == CommonEnum.EngineeringOrderStatus.RECEIVED) {
//                    edt_order_statuse_colon.setText("[已经接收]");
                    isClick = false;
                } else if (engineeringOrderStatus == CommonEnum.EngineeringOrderStatus.RETURNED) {
//                    edt_order_statuse_colon.setText("[退回]");
                    isClick = false;
                } else if (engineeringOrderStatus == CommonEnum.EngineeringOrderStatus.COMPLETED) {
//                    edt_order_statuse_colon.setText("[竣工");
                    isClick = false;
                }
            }


//            if (engineeringInstallationOrderBean.isHasPauseOrder()) {
//                rl_wether_pause_colon.setVisibility(View.VISIBLE);
//                edt_wether_pause_colon.setText("{[暂停]");
//            } else
//                rl_wether_pause_colon.setVisibility(View.GONE);

            if (null != engineeringInstallationOrderBean.getProductList()) {
                productBeanList.clear();
                productBeanList.addAll(engineeringInstallationOrderBean.getProductList());
                adapter.setData(productBeanList);
                adapter.notifyDataSetChanged();
            }
        }

        btnClickable();
    }

    private void showEngineeringInfoDialog() {
        if (ll_engineering_info_content.getVisibility() == View.VISIBLE) {
            ll_engineering_info_content.setVisibility(View.GONE);
            img_project_select.setBackgroundResource(R.mipmap.arrow_down_icon);
        } else {
            ll_engineering_info_content.setVisibility(View.VISIBLE);
            img_project_select.setBackgroundResource(R.mipmap.arrow_top);
        }
    }

    private void btnClickable() {
        if (isClick) {
            ll_receive_back_btn.setVisibility(View.VISIBLE);
            btn_receive_order.setBackgroundResource(R.color.rgb_00B050);

            btn_back_order.setBackgroundResource(R.color.rgb_C00000);
        } else {
            ll_receive_back_btn.setVisibility(View.GONE);

//            btn_receive_order.setBackgroundResource(R.color.rgb_D9D9D9);
//
//            btn_back_order.setBackgroundResource(R.color.rgb_D9D9D9);
        }
    }


    private void receiveOrder() {
        status = CommonEnum.STATUS.RECEIVED;
        isClick = false;
        btnClickable();
        teamReceiveOrBackEngineeringInstallOrderPresenter.receiveOrbackEngineeringOrder();
    }

    private void backOrder() {
        status = CommonEnum.STATUS.RETURNED;
        isClick = false;
        btnClickable();
        teamReceiveOrBackEngineeringInstallOrderPresenter.receiveOrbackEngineeringOrder();
    }

    @Override
    public void onClick(View view) {
        Intent mIntent;
        switch (view.getId()) {
            // 项目名称 收放
            case R.id.img_project_select:
                showEngineeringInfoDialog();
                break;
            // 接收工单
            case R.id.btn_receive_order:
                if (isClick)
                    receiveOrder();
                break;
            // 退回工单
            case R.id.btn_back_order:
                if (isClick)
                    backOrder();
                break;
            // 勘察内容
            case R.id.tv_survey_content:
                mIntent = new Intent(getActivity(), TeamEngineeringInstallWorkorderInfoSurveyActivity.class);
                startActivity(mIntent);
                break;

        }
    }


    @Override
    public String getOrderId() {
        if (null != engineeringInstallationOrderBean)
            return engineeringInstallationOrderBean.getId();
        return "";
    }

    @Override
    public CommonEnum.STATUS getStatus() {
        return status;
    }

    @Override
    public void receiveOrBackInstallOrderSuccessed() {
        if (null != status) {
            if (status == CommonEnum.STATUS.RECEIVED)
                Toast.makeText(getActivity(), "接收成功！", Toast.LENGTH_SHORT).show();
            else if (status == CommonEnum.STATUS.RETURNED)
                Toast.makeText(getActivity(), "退回成功！", Toast.LENGTH_SHORT).show();
        }
        EventBus.getDefault().post(new EngineeringOrderDetailForTeamEvent());
        isClick = false;
        btnClickable();
    }

    @Override
    public void receiveOrBackInstallOrderFailed(String desc) {
        Toast.makeText(getActivity(), desc, Toast.LENGTH_SHORT).show();
        isClick = true;
        btnClickable();
    }

    @Override
    public void showLoading() {

    }

    @Override
    public void hideLoading() {

    }
}
